package com.gmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig_Delay {

    public static final String Exchange_UnPay_direct = "Exchange_UnPay_direct";
    public static final String Queue_UnPay = "Queue_UnPay";
    public static final String UnPay_RoutingKey = "UnPay_RoutingKey";

    public static final String Exchange_UnPay_Delay_direct = "Exchange_UnPay_Delay_direct";
    public static final String Queue_UnPay_Delay = "Queue_UnPay_Delay";
    public static final String UnPay_Delay_RoutingKey = "UnPay_Delay_RoutingKey";


    @Bean(Exchange_UnPay_direct)
    public Exchange exchange_Business_delay_1(){
        return ExchangeBuilder.directExchange(Exchange_UnPay_direct).build();
    }

    @Bean(Queue_UnPay)
    public Queue queue_Business_delay_1(){
        return QueueBuilder.durable(Queue_UnPay)
                //.withArgument("x-dead-letter-exchange",Exchange_DeadLetter_1)
                //.withArgument("x-dead-letter-routing-key",Business_RoutingKey_1)
                //.withArgument("x-message-ttl", 10000)
                .deadLetterExchange(Exchange_UnPay_Delay_direct)
                .deadLetterRoutingKey(UnPay_Delay_RoutingKey)
                .ttl(1000*30)//ms
                .build();
    }

    @Bean
    public Binding binding_business_delay_1(
            @Qualifier(Queue_UnPay) Queue queue,
            @Qualifier(Exchange_UnPay_direct) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(UnPay_RoutingKey).noargs();
    }

    @Bean(Exchange_UnPay_Delay_direct)
    public Exchange Exchange_Delay_1(){
        return ExchangeBuilder.directExchange(Exchange_UnPay_Delay_direct).build();
    }

    @Bean(Queue_UnPay_Delay)
    public Queue Queue_Delay_1(){
        return QueueBuilder.durable(Queue_UnPay_Delay).build();
    }

    @Bean
    public Binding binding_delay_2(
            @Qualifier(Queue_UnPay_Delay) Queue queue,
            @Qualifier(Exchange_UnPay_Delay_direct) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(UnPay_Delay_RoutingKey).noargs();
    }
}
